<?php
class EngageCore {
		
	var $site = array();
	function set_vars($array){
		
		$settings = array();
		include("settings.php");
		foreach($settings as $key => $value){
			$this->site[$key] = $value;
		}
		$settings = NULL;
		
		if(count($array) > 0){
			$query = $array['query'];
		}else{
			$query = "index";
		}
		
		$this->site['uri'] = explode("/", $query);
		
		if(!isset($this->site['uri'][1])){
			$this->site['uri'][1] = "index";
		}
		
		$this->site['modules']['url'] = $this->site['url']."site/modules/";
		$this->site['modules']['root'] = $this->site['root']."site/modules/";
		$this->site['interface']['url'] = $this->site['url']."engine/interface/";
		$this->site['interface']['root'] = $this->site['root']."engine/interface/";
		
		if($this->site['uri'][0] == "interface"){	
			$this->site['module']['name'] = "EngageInterface";
			$this->site['module']['url'] = $this->site['interface']['url'];
			$this->site['module']['root'] = $this->site['interface']['root'];
			$this->site['module']['views']['url'] = $this->site['interface']['url']."/views/";
			$this->site['module']['views']['root'] = $this->site['interface']['root']."/views/";
			$this->site['module']['plugin']['url'] = $this->site['interface']['url']."/plugin/";
			$this->site['module']['plugin']['root'] = $this->site['interface']['root']."/plugin/";
		}else{			
			$this->site['module']['name'] = $this->site['uri'][0];
			$this->site['module']['url'] = $this->site['modules']['url'].$this->site['uri'][0]."/";
			$this->site['module']['root'] = $this->site['modules']['root'].$this->site['uri'][0]."/";
			$this->site['module']['views']['url'] = $this->site['modules']['url'].$this->site['uri'][0]."/views/";
			$this->site['module']['views']['root'] = $this->site['modules']['root'].$this->site['uri'][0]."/views/";
			$this->site['module']['plugin']['url'] = $this->site['modules']['url'].$this->site['uri'][0]."/plugin/";
			$this->site['module']['plugin']['root'] = $this->site['modules']['root'].$this->site['uri'][0]."/plugin/";
			
		}
		$this->site['current']['url'] = $this->site['url'].$query;
		$this->site['classes'] = $this->get_declared_plugins_helpers();
		$this->site['actions'] = array();
	}
	
	function get_declared_plugins_helpers(){
		$ret = array();
		
		$helpers_url = $this->site['root']."engine/helpers/";
		$helpers_dir =dir($helpers_url);
		
		while($helper_file = $helpers_dir->read()) {
			
			if ($helper_file == "." || $helper_file == ".."){
				$n = "";
			}else{
				$helper_url = $this->site['root']."engine/helpers/".$helper_file;
				if (file_exists($helper_url)) {
					$name = str_replace(".php", "", $helper_file);
					$ret[] = $name;
				}
			}
			
		}
		
		$helpers_dir->close();
		
		$modules_dir = dir($this->site['modules']['root']);
		
		while($folder = $modules_dir->read()) {
			
			if ($folder == "." || $folder == ".."){
				$n = "";
			}else{
				$plugin_url = $this->site['modules']['root'].$folder."/plugin/start.php";
				if (file_exists($plugin_url)) {
					$name = $folder."Plugin";
					$ret[] = $name;
				}
			}
			
		}
		
		$modules_dir->close();
		
		$ret[] = $this->site['module']['name'];
		
		return $ret;
	}
	
	function event($action, $vars = NULL){
		$ret = array();
		$classes = $this->site['classes']; 
		$method_name = "_events_";
		if(in_array($action, $this->site['actions'])){
			return false;
		}else{
			$this->site['actions'][] = $action;
			foreach($classes as $id => $name){
				if(method_exists($name, $method_name)){
					$class = new $name;
					$ret[] = $class->$method_name($action, $vars);
					$class = NULL;
				}
			}
			return $ret;
		}
	}
}
?>